package com.easou.sdk.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.easou.sdk.model.dianjin.DianJinPaymentObj;
import com.easou.sdk.partner.DianJinIOSSdk;
import com.easou.sdk.service.PartnerService;
import com.easou.sdk.service.PartnerServiceFactory;
import com.easou.sdk.utils.json.Json;

@Controller
public class DianJinIOSController {
	private static Logger LOG = Logger.getLogger(DianJinIOSController.class);
	
	@Autowired
	private PartnerServiceFactory serviceFactory;

	@RequestMapping(value = "/webApi/djIOSPayment.do", method = RequestMethod.POST)
	public ModelAndView payCallback(HttpServletRequest req, HttpServletResponse res) {
		PartnerService ps = serviceFactory.getBean(DianJinIOSSdk.instance().getPartnerId());
		DianJinPaymentObj data = new DianJinPaymentObj();
		data.setAppId(req.getParameter("App_Id"));
		data.setCreateTime(req.getParameter("Create_Time"));
		data.setExtra(req.getParameter("Extra"));
		data.setRechargeMoney(req.getParameter("Recharge_Money"));
		data.setPayStatus(req.getParameter("Pay_Status"));
		data.setRechargeGoldCount(req.getParameter("Recharge_Gold_Count"));
		data.setSign(req.getParameter("Sign"));
		data.setUin(req.getParameter("Uin"));
		data.setUrechargeId(req.getParameter("Urecharge_Id"));
		LOG.info("DianJinIOSPaymentObj info:"+Json.toJson(data));
		try {
			if(StringUtils.isNotBlank(data.getSign()) 
					&& StringUtils.isNotBlank(data.getPayStatus()) 
					&& StringUtils.isNotBlank(data.getUrechargeId()) && ps.doPayment(data)){
				res.getWriter().print("Success");
			}else{
				res.getWriter().print("Error");
			}
		} catch (Exception e) {
			LOG.error("DianJinPaymentObj payment error!",e);
		}
		return null;
	}
	
}
